package com.dzl.exchange.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.database.Master;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.exchange.dao.WelfareExchangeGoodsMapper;
import com.dzl.exchange.pojo.WelfareExchange;
import com.dzl.exchange.pojo.WelfareExchangeGoods;
import com.dzl.exchange.pojo.dto.ExchangeGoodsSkuDto;
import com.dzl.exchange.pojo.dto.GoodsSkuDto;
import com.dzl.exchange.pojo.vo.ExchangeSkuGoodsVo;
import com.dzl.exchange.service.WelfareExchangeGoodsService;
import com.dzl.exchange.service.WelfareExchangeService;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.service.WelfareGoodsAttrService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兑换券组关联商品表(WelfareExchangeGroupGoods)表服务实现类
 * @author caobucao
 * @date 2021-07-09 17:30:33
 */
@Service
public class WelfareExchangeGoodsServiceImpl extends BaseServiceImpl<WelfareExchangeGoods> implements WelfareExchangeGoodsService {

    @Autowired
    private WelfareExchangeGoodsMapper mapper;
    @Autowired
    private WelfareExchangeService welfareExchangeService;
    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private WelfareGoodsAttrService welfareGoodsAttrService;

    @Override
    public Integer updateExchangeGoodsDisplayPrice(Integer exchangeGoodsId, BigDecimal displayPrice){
        WelfareExchangeGoods welfareExchangeGoods = new WelfareExchangeGoods();
        welfareExchangeGoods.setDisplayPrice(displayPrice);
        welfareExchangeGoods.setId(exchangeGoodsId);
        return mapper.updateByPrimaryKeySelective(welfareExchangeGoods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Master
    @Override
    public Map<String, Object> addExchangeGoodsSku(WelfareExchange welfareExchange, ExchangeGoodsSkuDto dto) {
        Map<String, Object> map = new HashMap<>();
        welfareExchangeService.updateByPrimaryKeySelective(welfareExchange);
        List<GoodsSkuDto> skuGoodsList = dto.getSkuGoodsList();
        List<WelfareExchangeGoods> list = new ArrayList<>();
        Example example = new Example(WelfareExchangeGoods.class);
        example.createCriteria().andEqualTo("exchangeGroupId", welfareExchange.getId());
        mapper.deleteByExample(example);

        StringBuffer goodsNameBuf = new StringBuffer();
        StringBuffer skuBuf = new StringBuffer();
        StringBuffer skuQuantityBuf = new StringBuffer();
        skuGoodsList.forEach(x -> {
            Integer skuId = x.getSkuId();
            //校验商品是否是上架状态
            Integer goodsId = x.getGoodsId();
            WelfareGoods welfareGoods = welfareGoodsService.selectByPrimaryKey(goodsId);
            if(!welfareGoods.getGoodsStatus().equals(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode())) {
                String goodsName = welfareGoods.getGoodsName();
                goodsNameBuf.append(goodsName).append("<nb/>");
            } else {
                //校验SKU是否是删除状态
                WelfareGoodsAttr welfareGoodsAttr = welfareGoodsAttrService.selectByPrimaryKey(skuId);
                if(welfareGoodsAttr.getStatus().equals(0)) {
                    Integer skuGoodsId = welfareGoodsAttr.getGoodsId();
                    WelfareGoods welfareSkuGoods = welfareGoodsService.selectByPrimaryKey(skuGoodsId);
                    skuBuf.append(welfareSkuGoods.getGoodsName()).append("的");
                    if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getFirstDetailName())) {
                        skuBuf.append(welfareGoodsAttr.getFirstDetailName());
                    }
                    if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getSecondDetailName())) {
                        skuBuf.append("-").append(welfareGoodsAttr.getSecondDetailName());
                    }
                    if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getThirdDetailName())) {
                        skuBuf.append("-").append(welfareGoodsAttr.getThirdDetailName());
                    }
                    skuBuf.append("<nb/>");
                } else {
                    //规格不是删除的才查库存是否充足
                    //检查sku库存是否充足
                    if(welfareGoodsAttr.getQuantity()<=0) {
                        Integer skuGoodsId = welfareGoodsAttr.getGoodsId();
                        WelfareGoods welfareSkuGoods = welfareGoodsService.selectByPrimaryKey(skuGoodsId);
                        skuQuantityBuf.append(welfareSkuGoods.getGoodsName()).append("的");
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getFirstDetailName())) {
                            skuQuantityBuf.append(welfareGoodsAttr.getFirstDetailName());
                        }
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getSecondDetailName())) {
                            skuQuantityBuf.append("-").append(welfareGoodsAttr.getSecondDetailName());
                        }
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getThirdDetailName())) {
                            skuQuantityBuf.append("-").append(welfareGoodsAttr.getThirdDetailName());
                        }
                        skuQuantityBuf.append("<nb/>");
                    }
                }
            }

            WelfareExchangeGoods welfareExchangeGoods = new WelfareExchangeGoods();
            welfareExchangeGoods.setExchangeGroupId(dto.getExchangeGroupId());
            welfareExchangeGoods.setStatus(1);
            welfareExchangeGoods.setGoodsId(goodsId);
            welfareExchangeGoods.setSkuId(skuId);
            welfareExchangeGoods.setDisplayPrice(x.getDisplayPrice());
            list.add(welfareExchangeGoods);
        });
        mapper.insertList(list);
        if(goodsNameBuf.length()>0) {
            goodsNameBuf.delete(goodsNameBuf.length()-5, goodsNameBuf.length());
            throw new CommonException(4000, "下列商品已下架，请手动移除后再保存：<br/>"+goodsNameBuf.toString());
        }
        if(skuBuf.length()>0) {
            skuBuf.delete(skuBuf.length()-5, skuBuf.length());
            throw new CommonException(4000, "下列商品规格状态发生变更，请检查后保存：<br/>"+skuBuf.toString());
        }
        if(skuQuantityBuf.length()>0) {
            skuQuantityBuf.delete(skuQuantityBuf.length()-5, skuQuantityBuf.length());
            throw new CommonException(4000, "下列商品规格库存不足，请检查后保存：<br/>"+skuQuantityBuf.toString());
        }
        return map;
    }


    @Override
    public List<ExchangeSkuGoodsVo> selectExchangeSkuGoodsVoByExchangeGroupId(Integer exchangeGroupId){
        return mapper.selectExchangeSkuGoodsVoByExchangeGroupId(exchangeGroupId);
    }

    @Override
    public List<ExchangeSkuGoodsVo> selectExchangeSkuGoodsVoByExchangeGoodsIds(String exchangeGoodsIds){
        return mapper.selectExchangeSkuGoodsVoByExchangeGoodsIds(exchangeGoodsIds);
    }
}
